Stockname = char(Stockname);
Indexname = char(Indexname);
[StockName IndexName Date PriceStock PriceIndex] = getStock_Index(Stockname, Indexname);
%Stima EWMA
rStock=PriceStock(2:end)./PriceStock(1:end-1)-1;
rIndex=PriceIndex(2:end)./PriceIndex(1:end-1)-1;

%Lambda=0.94;
%-----------------------------------------------------------
%Estimation EWMA
%-----------------------------------------------------------
 %Rolling window
rolling=260;
[n,m]=size(rStock);
k=n-rolling;
datan = x2mdate(Date,0);
datam=datan(262:end);

%Starting Point
rStockEWMA=rStock(1:260,:);
rIndexEWMA=rIndex(1:260,:);
varstock(1)=var(rStockEWMA,1); 
varindex(1)=var(rIndexEWMA,1); 
covariance=cov(rStockEWMA,rIndexEWMA);
covar(1)=covariance(1,2);
betaEWMA(1)=covar(1)/varindex(1);
rStockEWMA=rStock(260:end,:);
rIndexEWMA=rIndex(260:end,:);
for i=1:k
    j=i+1;
    varstock(j)=varstock(i).*Lambda +(1-Lambda)*(rStockEWMA(i).^2);
    varindex(j)=varindex(i).*Lambda +(1-Lambda)*(rIndexEWMA(i).^2);
    covar(j)=covar(i).*Lambda +(1-Lambda)*(rStockEWMA(i)*rIndexEWMA(i));
    betaEWMA(j)=covar(j)/ varindex(j);
end 

betaSmoothing = betaEWMA(2:end)';
%---Output to Excell
ExcelSmoothing=[datam,betaSmoothing];